package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;
import sun.security.x509.Extension;
import sun.security.x509.KeyUsageExtension;
import tr.gov.tubitak.bilgem.esya.certviewer.Bundle;
import tr.gov.tubitak.bilgem.esya.certviewer.EStringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <pre>
 * KeyUsage ::= BIT STRING
 * {
 *     digitalSignature        (0),
 *     nonRepudiation          (1),
 *     keyEncipherment         (2),
 *     dataEncipherment        (3),
 *     keyAgreement            (4),
 *     keyCertSign             (5),
 *     cRLSign                 (6),
 *     encipherOnly            (7),
 *     decipherOnly            (8) }
 * </pre>
 *
 */

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 01.12.2011
 * Time: 09:57
 * To change this template use File | Settings | File Templates.
 */
public class EKeyUsage implements ECertExtension{
    KeyUsageExtension keyUsageExtension;
    final String KEY_USAGE_DIGITAL_SIGNATURE="digitalSignature";
    final String KEY_USAGE_NON_REPUDIATION="nonRepudiation";
    final String KEY_USAGE_KEY_ENCIPHERMENT="keyEncipherment";
    final String KEY_USAGE_DATA_ENCIPHERMENT="dataEncipherment";
    final String KEY_USAGE_KEY_AGGREMENT="keyAgreement";
    final String KEY_USAGE_KEY_CERT_SIGN="keyCertSign";
    final String KEY_USAGE_CRL_SIGN="cRLSign";
    final String KEY_USAGE_ENCIPHER_ONLY="encipherOnly";
    final String KEY_USAGE_DECIPHER_ONLY="decipherOnly";


    final String KEY_USAGE_DIGITAL_SIGNATURE_TITLE=Bundle.getString("EKeyUsage.DigitalSignature");
    final String KEY_USAGE_NON_REPUDIATION_TITLE=Bundle.getString("EKeyUsage.NonRepudiation");
    final String KEY_USAGE_KEY_ENCIPHERMENT_TITLE=Bundle.getString("EKeyUsage.KeyEncipherment");
    final String KEY_USAGE_DATA_ENCIPHERMENT_TITLE=Bundle.getString("EKeyUsage.DataEncipherment");
    final String KEY_USAGE_KEY_AGGREMENT_TITLE=Bundle.getString("EKeyUsage.KeyAggrement");
    final String KEY_USAGE_KEY_CERT_SIGN_TITLE=Bundle.getString("EKeyUsage.CertSign");
    final String KEY_USAGE_CRL_SIGN_TITLE=Bundle.getString("EKeyUsage.CrlSign");
    final String KEY_USAGE_ENCIPHER_ONLY_TITLE=Bundle.getString("EKeyUsage.EncipherOnly");
    final String KEY_USAGE_DECIPHER_ONLY_TITLE=Bundle.getString("EKeyUsage.DecipherOnly");

    public EKeyUsage(boolean[] bitValue) {
        this.bitValue = bitValue;
    }

    boolean [] bitValue;

    public List<String> getValueList() throws Exception
       {
           List<String> valueList = new ArrayList<String>();
           if(bitValue.length!=9)
           {
               return valueList;
           }
           if(bitValue[0])
               valueList.add(KEY_USAGE_DIGITAL_SIGNATURE);
           if(bitValue[1])
               valueList.add(KEY_USAGE_NON_REPUDIATION);
           if(bitValue[2])
               valueList.add(KEY_USAGE_KEY_ENCIPHERMENT);
           if (bitValue[3])
               valueList.add(KEY_USAGE_DATA_ENCIPHERMENT);
           if (bitValue[4])
               valueList.add(KEY_USAGE_KEY_AGGREMENT);
           if (bitValue[5])
               valueList.add(KEY_USAGE_KEY_CERT_SIGN);
           if (bitValue[6])
               valueList.add(KEY_USAGE_CRL_SIGN);
           if (bitValue[7])
               valueList.add(KEY_USAGE_ENCIPHER_ONLY);
           if (bitValue[8])
           valueList.add(KEY_USAGE_DECIPHER_ONLY);
           return valueList;
       }

       public String toItemListTitle()
       {
           String retStr = "<html><ul>";
           try {
               List<String> valueList = getValueListTitle();
               for (String s : valueList) {
                   retStr+="<li>"+s+"</li>";
               }
           } catch (Exception e) {
               e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
           }
           retStr+="</ul></html>";
           return retStr;
       }

         public List<String> getValueListTitle() throws Exception
       {
           List<String> valueList = new ArrayList<String>();
           if(bitValue.length!=9)
           {
               return valueList;
           }
           if(bitValue[0])
               valueList.add(KEY_USAGE_DIGITAL_SIGNATURE_TITLE);
           if(bitValue[1])
               valueList.add(KEY_USAGE_NON_REPUDIATION_TITLE);
           if(bitValue[2])
               valueList.add(KEY_USAGE_KEY_ENCIPHERMENT_TITLE);
           if (bitValue[3])
               valueList.add(KEY_USAGE_DATA_ENCIPHERMENT_TITLE);
           if (bitValue[4])
               valueList.add(KEY_USAGE_KEY_AGGREMENT_TITLE);
           if (bitValue[5])
               valueList.add(KEY_USAGE_KEY_CERT_SIGN_TITLE);
           if (bitValue[6])
               valueList.add(KEY_USAGE_CRL_SIGN_TITLE);
           if (bitValue[7])
               valueList.add(KEY_USAGE_ENCIPHER_ONLY_TITLE);
           if (bitValue[8])
            valueList.add(KEY_USAGE_DECIPHER_ONLY_TITLE);
           return valueList;
       }

    @Override
    public String getName() {
       return Bundle.getString("ECertField.KeyUsage");
    }

    @Override
    public String getShortTitle() {
        try {
            return EStringUtils.join(getValueListTitle().iterator(), ",");
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return "";
    }

    @Override
    public String getLongTitle() {
                try {
            return EStringUtils.join(getValueListTitle().iterator(), "\n");
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return "";
    }

    @Override
    public boolean isCritical() {
        return keyUsageExtension.isCritical();
    }

    @Override
    public void encode(byte[] data) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setAsnObject(Object asnObject) {
        if(asnObject instanceof KeyUsageExtension)
        {
            keyUsageExtension = (KeyUsageExtension) asnObject;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
